package com.cn.xact.ty_treasured_book.activity;

import com.cn.xact.ty_treasured_book.R;
import com.cn.xact.ty_treasured_book.net.NetworkCallBack;
import com.cn.xact.ty_treasured_book.net.NetworkImpl;
import com.cn.xact.ty_treasured_book.util.UIHelper;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class HomePSWSettingsActivity extends Activity {

    private EditText mCurPSW = null;
    private EditText mNewPSW = null;
    private EditText mNewPSWConfirm = null;
    private EditText mVCode = null;
    private TextView mChangeVCode = null;
    private ImageView mVCodeImg = null;

    private NetworkImpl mPSWSettingsNetwork;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_home_pswsettings);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_customer_title);

        ImageView tmp = (ImageView)findViewById(R.id.common_title_indicator);
        tmp.setImageResource(R.drawable.main_home);
        TextView title1 = (TextView)findViewById(R.id.common_title_first_section);
        title1.setText(R.string.title_activity_home);
        TextView separator1 = (TextView)findViewById(R.id.common_title_first_separator);
        separator1.setVisibility(View.VISIBLE);
        TextView title2 = (TextView)findViewById(R.id.common_title_second_section);
        title2.setText(R.string.home_user_change_psw);
        title2.setVisibility(View.VISIBLE);

        title1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                HomePSWSettingsActivity.this.finish();
            }

        });

        mPSWSettingsNetwork = new NetworkImpl(this, new MyNetworkCallBack());

        mCurPSW = (EditText)findViewById(R.id.home_user_change_psw_cur_psw);
        mNewPSW = (EditText)findViewById(R.id.home_user_change_psw_new_psw_et);
        mNewPSWConfirm = (EditText)findViewById(R.id.home_user_change_psw_new_psw_confirm);
        mVCode = (EditText)findViewById(R.id.home_user_change_psw_cerCode);
        mChangeVCode = (TextView)findViewById(R.id.home_user_change_psw_cerCode_change);
        mChangeVCode.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        mChangeVCode.getPaint().setAntiAlias(true);
        mChangeVCode.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mPSWSettingsNetwork.getVerifyCodeAsync();
            }

        });
        mVCodeImg = (ImageView)findViewById(R.id.home_user_change_psw_cerCode_img);

        Button okbt = (Button)findViewById(R.id.home_user_change_psw_confirmed);
        okbt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if ((mCurPSW.getText().length() <= 0)
                        || (mNewPSW.getText().length() <= 0)
                        || (mNewPSWConfirm.getText().length() <= 0)
                        || !(mNewPSW.getText().toString().equals(mNewPSWConfirm.getText().toString()))){
                    showFailed(getResources().getString(R.string.home_user_settings_change_psw_failed));
                    return;
                } else if (mVCode.getText().length() <= 0) {
                    showFailed(getResources().getString(R.string.home_user_settings_change_psw_vcode_failed));
                    return;
                }

                mPSWSettingsNetwork.changePSWAsync(123, mNewPSW.getText().toString(), mVCode.getText().toString());
            }

        });
    }

    private void showFailed(String msg) {
        AlertDialog.Builder build = new AlertDialog.Builder(this);
        build.setMessage(msg);
        build.setPositiveButton(getResources().getString(R.string.OK), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        build.show();
    }

    private void showComplete() {
        AlertDialog.Builder build = new AlertDialog.Builder(this);
        build.setMessage(getResources().getString(R.string.home_user_settings_succeed));
        build.setPositiveButton(getResources().getString(R.string.OK), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                HomePSWSettingsActivity.this.finish();
            }
        });

        build.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home_pswsettings, menu);
        return true;
    }

    class MyNetworkCallBack extends NetworkCallBack {
        public void onNetworkCallBack(int id, boolean success, Object attach1, Object attach2) {
            if (!success) {
                UIHelper.showConfirmDialog(HomePSWSettingsActivity.this, null, getResources().getString(R.string.server_busy));
                return;
            }
            switch(id) {
            case NetworkImpl.NET_WORK_ID_SET_USER_PSW:
                if (success) {
                    showComplete();
                } else {
                    showFailed((String)attach1);
                }
                break;
            case NetworkImpl.NET_WORK_ID_GET_VERIFY_CODE:
                break;
            }
        }
    }

}
